package com.yc.vcode;

import com.yc.vcode.bean.UploadProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/19
 * Email haijunzhou@hnit.edu.cn
 */
@AutoConfiguration
@ComponentScan("com.yc.vcode")
@ConditionalOnProperty(prefix = "upload",name = "enable",havingValue = "true")
@Slf4j
public class UploadConfig implements WebMvcConfigurer {

    @Autowired
    UploadProperties properties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        properties.getMapper().forEach(mapper->{
            String webPath = mapper.getWebPath();
            String disPath = mapper.getDiskPath();
            registry.addResourceHandler(webPath + "**")
                    .addResourceLocations("file:" + disPath);
            File file = new File(disPath);
            if (file.exists() == false) {
                if (file.mkdirs() == false) {
                    log.debug("创建目录失败: " + disPath);
                }
            }
        });
    }

    // 当前配置类对象的初始化方法
    // init-method   destory-method
//    @PostConstrutor
//    void init(){
//
//    }
}
